<template>
  <a-modal
    title="新增发布管理"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
<!--        <a-form-item-->
<!--          label="图片路径"-->
<!--          :labelCol="labelCol"-->
<!--          :wrapperCol="wrapperCol"-->
<!--          has-feedback-->
<!--        >-->
<!--          <a-input placeholder="请输入图片路径" v-decorator="['imagePath', {rules: [{required: true, message: '请输入图片路径！'}]}]" />-->
<!--        </a-form-item>-->
        <a-form-item
          label="列表名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入列表名称" v-decorator="['lineName', {rules: [{required: true, message: '请输入列表名称！'}]}]" />
        </a-form-item>
        <a-form-item
          label="分辨率"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入分辨率" v-decorator="['distinguishability', {rules: [{required: true, message: '请输入分辨率！'}]}]" />
        </a-form-item>
        <a-form-item
          label="终端类型"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select allow-clear placeholder="请选择终端类型" >
            <a-select-option v-for="(item,index) in tempInfo" :key="index" :value="item.id" @click="selectedValue(item.dictName)">{{ item.dictName }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          label="播放时长(秒)"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input-number style="width: 100%" placeholder="请输入播放时长" v-decorator="['playTime', {rules: [{required: true, message: '请输入播放时长！'}]}]" />
        </a-form-item>
        <a-form-item
          label="声音输出"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select allow-clear placeholder="请选择声音是否输出" >
            <a-select-option v-for="(item,index) in tempVoiceInfo" :key="index" :value="item.id" @click="selectVoiceStatus(item.dictName)">{{ item.dictName }}</a-select-option>
          </a-select>
<!--          <a-input placeholder="请输入声音输出" v-decorator="['voiceOutput', {rules: [{required: true, message: '请输入声音输出！'}]}]" />-->
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { pushManageAdd } from '@/api/modular/system/pushManageManage'
  import { getChildInfo } from '@/api/modular/system/fenDictManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        tempInfo: {},
        dictName: '',
        dictVoiceName: '',
        tempVoiceInfo: {}
      }
    },
    // created () {
    //   this.visible = true
    // },
    methods: {
      // 初始化方法
      add (record) {
        getChildInfo({ 'dictCode': 'zdlx' }).then(res => {
          this.tempInfo = res.data
        })
        getChildInfo({ 'dictCode': 'SYSC' }).then(res => {
          this.tempVoiceInfo = res.data
        })
        this.visible = true
      },
      selectedValue(dictName) {
        this.dictName = dictName
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values['terminalType'] = this.dictName
            values['voiceOutput'] = this.dictVoiceName
            pushManageAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败')// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      selectVoiceStatus(dictName) {
        this.dictVoiceName = dictName
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
